package linkLIstAlgorithm;

/*
 * Author: JacksonCoder
 * FileName: reversList
 * Version: 1.0.0
 * Date: 2025/4/29
 */

class ListNode{
    int val;
    ListNode next = null;
    public ListNode(int val){
        this.val = val;
    }
}

public class reversList {
    public ListNode ReverseList(ListNode head){
        if(head == null){
            return null;
        }
        ListNode dummy = null;
        ListNode pre = null;
        while(head != null){
            dummy = head.next;
            head.next = pre;
            pre = head;
            head = dummy;
        }
        return pre;
    }
    public static void main(String[] args) {
        // 测试案例
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);
        reversList reversList = new reversList();
        ListNode reversedHead = reversList.ReverseList(head);
        while (reversedHead != null) {
            System.out.print(reversedHead.val + " ");
            reversedHead = reversedHead.next;
        }

    }
}
